<?php

class gerenClassAction extends Action
{
    public function getinitAjax()
    {
        $uid = $this->adminid;
        $carr = m('admin')->getcompanyinfo($uid);

        return array(
            'gerentodo' => $this->option->getval('gerennotodo_' . $uid . ''),
            'qmimgstr' => $this->option->getval('qmimgstr_' . $uid . ''),
            'carr' => $carr,
        );
    }

    public function cogsaveAjax()
    {
        $uid = $this->adminid;
        $this->option->setval('gerennotodo_' . $uid . '', $this->get('gerentodo', '0'));
    }

    //保存图片
    public function qmimgsaveAjax()
    {
        $uid = $this->adminid;
        $str = '';
        $qmimgstr = $this->post('qmimgstr');
        if (!isempt($qmimgstr)) {
            if (contain($qmimgstr, '.')) {
                $str = $qmimgstr;
            } else {
                $qma = explode(',', $qmimgstr);
                $str = '' . UPDIR . '/' . date('Y-m') . '/' . $uid . 'qming_' . rand(1000, 9999) . '.png';
                $this->rock->createtxt($str, base64_decode($qma[1]));
            }
        }
        $this->option->setval('qmimgstr_' . $uid . '', $str);
    }

    public function filebefore($table)
    {
        $key = $this->post('key');
        $atype = $this->post('atype');
        $where = 'and optid=' . $this->adminid . '';
        if ($atype == 'all') {
            $where = '';
            if ($this->adminid > 1) $where = m('admin')->getcompanywhere(3);
        }
        if ($key != '') {
            $where .= " and (`optname` like '%$key%' or `filename` like '%$key%' or `mtype`='$key')";
        }
        return array(
            'where' => $where,
            //'fields' => '`id`,fileext,filename,filesizecn,thumbpath,filepath,adddt,optname,downci,ip,web,mtype,mid',
        );
    }

//回收站数据
    public function getfilebefore($table)
    {
        $key = $this->post('key');
        $atype = $this->post('atype');
        $where = 'and optid=' . $this->adminid . '' . ' and status=-1';
        if ($atype == 'all') {
            $where = '';
            if ($this->adminid > 1) $where = m('admin')->getcompanywhere(3);
        }
        if ($key != '') {
            $where .= " and (`optname` like '%$key%' or `filename` like '%$key%' or `mtype`='$key')";
        }
        return array(
            'where' => $where,
            //'fields' => '`id`,fileext,filename,filesizecn,thumbpath,filepath,adddt,optname,downci,ip,web,mtype,mid',
        );
    }

    public function fileafter($table, $rows)
    {
        $fobj = m('file');
        foreach ($rows as $k => &$rs) {
            $rs['thumbpath'] = $fobj->getthumbpath($rs);
            $fpath = $rs['filepath'];

            $status = 1;
            if (substr($fpath, 0, 4) == 'http') {
                $status = 2;
            } else {
                if (isempt($rs['filenum']) && !file_exists($fpath)) $status = 0;
            }
            $rs['status'] = $status;
        }
        return array(
            'rows' => $rows
        );
    }

    public function delfileAjax()
    {
        $id = c('check')->onlynumber($this->post('id', '0'));
        m('file')->delfile($id);
        backmsg();
    }
    //修改彻底删除
    public function dodelfileAjax()
    {
        $id = c('check')->onlynumber($this->post('id', '0'));
        m('file')->delfile($id);//删除文件
        m('word')->delete("fileid in ($id)");//删除记录
        backmsg();
    }
    public function defaultAction()
    {
        $this->title = '修改头像';
        $face = $this->db->getmou($this->T('admin'), 'face', "`id`='$this->adminid'");
        $imgurl = '';
        if (!$this->rock->isempt($face)) {
            $imgurl = '../../' . preg_replace("/_crop\d{4}/", '', $face);
        }
        //$face			= $this->rock->repempt($face,'images/white.gif');
        $this->smartydata['face'] = $face;
        $this->smartydata['imgurl'] = $imgurl;
    }

    public function changestyleAjax()
    {
        $style = $this->post('style', '0');
        m('admin')->update('`style`=' . $style . '', 'id=' . $this->adminid . '');
    }


    public function editpassAjax()
    {
        $id = $this->adminid;
        if (getconfig('systype') == 'demo') exit('演示上不要修改');
        $oldpass = $this->rock->post('passoldPost');
        $pasword = $this->rock->post('passwordPost');
        $msg = '';
        if ($this->rock->isempt($pasword)) $msg = '新密码不能为空';
        if ($msg == '') {
            $oldpassa = $this->db->getmou($this->T('admin'), "`pass`", "`id`='$id'");
            if ($oldpassa != md5($oldpass)) $msg = '旧密码不正确';
            if ($msg == '') {
                if ($oldpassa == md5($pasword)) $msg = '新密码不能和旧密码相同';
            }
        }
        if ($msg == '') {
            if (!$this->db->record($this->T('admin'), "`pass`='" . md5($pasword) . "',`editpass`=`editpass`+1", "`id`='$id'")) $msg = $this->db->error();
        }
        if ($msg == '') $msg = 'success';
        echo $msg;
    }


    /**
     * 保存头像
     */
    public function savefaceAjax()
    {
        $id = $this->adminid;
        $arr = array('face' => $this->rock->post('facePost'));
        $msg = '';
        if (!$this->db->record($this->T('admin'), $arr, "`id`='$id'")) $msg = $this->db->error();
        if ($msg == '') $msg = 'success';
        echo $msg;
    }

    public function todoydAjax()
    {
        m('todo')->update("status=1,`readdt`='$this->now'", "`id` in(" . $this->post('s') . ") and `status`=0");
    }

    public function totaldaetods($table, $rows)
    {
        $wdtotal = m('todo')->rows("`uid`='$this->adminid' and `status`=0 and `tododt`<='$this->now'");
        return array('wdtotal' => $wdtotal);
    }

    public function beforetotaldaetods($table)
    {
        $s = " and `uid`='$this->adminid' and `tododt`<='$this->now'";
        return $s;
    }

    public function getlinksAjax()
    {
        $rows = m('links')->getrows('1=1', '*', '`type`,`sort`');
        echo json_encode($rows);
    }


    //导入个人通讯录
    public function piliangaddAjax()
    {
        $rows = c('html')->importdata('name,unitname,tel,mobile,email,gname,address', 'name');
        $oi = 0;
        $db = m('vcard');
        foreach ($rows as $k => $rs) {
            $rs['optdt'] = $this->now;
            $rs['uid'] = $this->adminid;
            $rs['optname'] = $this->adminname;
            $db->insert($rs);
            $oi++;
        }
        backmsg('', '成功导入' . $oi . '条数据');
    }

    public function filelogs_before($table)
    {
        $where = '';
        $fileid = (int)$this->post('fileid', '0');
        $where = "and `fileid`='$fileid'";
        return $where;
    }

    public function delfilelogsAjax()
    {
        $id = (int)$this->post('id', '0');
        m('files')->delete($id);
        backmsg();
    }
}